﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace meituan
{
    public partial class FormSetting : Form
    {
        public FormSetting()
        {
            InitializeComponent();
            string tempPath = Utils.TempPath("meituan_config");
            if (System.IO.File.Exists(tempPath))
            {
                string str = System.IO.File.ReadAllText(tempPath);
                Dictionary<string, string> config = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(str);
                if (config["isProxy"] == "1")
                {
                    radioButtonProxy2.Checked = true;
                    radioButtonProxy.Checked = false;
                }
                else
                {
                    radioButtonProxy.Checked = true;
                    radioButtonProxy2.Checked = false;
                }
                textBoxTimeMin.Text = config["timeMin"];
                textBoxTimeMax.Text = config["timeMax"];
                textBoxDataPath.Text = config["dataPath"];
            }
            else
            {
                buttonSave.Text = "初始化配置";
            }

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            string isProxy = "0";
            if (radioButtonProxy2.Checked == true)
            {
                isProxy = "1";
            }
            string timeMin = textBoxTimeMin.Text;
            string timeMax = textBoxTimeMax.Text;
            string dataPath = textBoxDataPath.Text;
            if (timeMin == "")
            {
                timeMin = "10";
            }
            if (timeMax == "")
            {
                timeMax = "20";
            }
            if (dataPath == "")
            {
                dataPath = @"D:\数据";
            }
            
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("isProxy", isProxy);
            dic.Add("timeMin", timeMin);
            dic.Add("timeMax", timeMax);
            dic.Add("dataPath", dataPath);

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
            string tempPath = Utils.TempPath("meituan_config");
            System.IO.File.WriteAllText(tempPath, json);
            MessageBox.Show("设置成功");
        }

        private void buttonDataPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string path = dialog.SelectedPath;
                textBoxDataPath.Text = path;
            }
        }

    }
}
